home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / libs / libelf-0.5 / libelf-0 / libelf-0.5.2 / 32.xlatetof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-19  |  9.6 KB  |  350 lines

  1. /*
  2. 32.xlatetof.c - implementation of the elf32_xlateto[fm](3) functions.
  3. Copyright (C) 1995 Michael Riepe <riepe@ifwsn4.ifw.uni-hannover.de>
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <private.h>
  21. #include <ext_types.h>
  22. #include <byteswap.h>
  23.  
  24. /*
  25.  * Ugly, ugly
  26.  */
  27. #define x
  28. #if defined/**/x
  29. # define Cat2(a,b)a##b
  30. # define Cat3(a,b,c)a##b##c
  31. # define Exn(m1,m2,args)m1##m2##args
  32. # define Ex1(m1,m2,a,b)m1##m2(a##b)
  33. # define Ex2(m1,m2,a,b,c)m1##m2(a,b##c)
  34. #else
  35. # define Cat2(a,b)a/**/b
  36. # define Cat3(a,b,c)a/**/b/**/c
  37. # define Exn(m1,m2,args)m1/**/m2/**/args
  38. # define Ex1(m1,m2,a,b)m1/**/m2(a/**/b)
  39. # define Ex2(m1,m2,a,b,c)m1/**/m2(a,b/**/c)
  40. #endif
  41. #undef x
  42.  
  43. /*
  44.  * auxiliary macros for execution order reversal
  45.  */
  46. #define seq_forw(a,b) a b
  47. #define seq_back(a,b) b a
  48.  
  49. /*
  50.  * function instantiator
  51.  */
  52. #define copy_type_e_io(name,e,io,tfrom,tto,copy)        \
  53.     static void                            \
  54.     Cat3(name,_,io)(char *dst, const char *src, size_t n) {    \
  55.     const tfrom *from = (const tfrom*)src;            \
  56.     tto *to = (tto*)dst;                    \
  57.     if (sizeof(tfrom) < sizeof(tto)) {            \
  58.         from += n;                        \
  59.         to += n;                        \
  60.         while (n-- > 0) {                    \
  61.         --from;                        \
  62.         --to;                        \
  63.         copy(e,io,seq_back)                \
  64.         }                            \
  65.     }                            \
  66.     else {                            \
  67.         while (n-- > 0) {                    \
  68.         copy(e,io,seq_forw)                \
  69.         from++;                        \
  70.         to++;                        \
  71.         }                            \
  72.     }                            \
  73.     }
  74.  
  75. #define copy_type_e(name,e,type,copy)                \
  76.     copy_type_e_io(name,e,tom,Cat2(__ext_,type),type,copy)    \
  77.     copy_type_e_io(name,e,tof,type,Cat2(__ext_,type),copy)
  78.  
  79. /*
  80.  * master function instantiator
  81.  */
  82. #define copy_type(name,version,type,copy)        \
  83.     copy_type_e(Cat3(name,L,version),L,type,copy)    \
  84.     copy_type_e(Cat3(name,M,version),M,type,copy)
  85.  
  86. /*
  87.  * scalar copying
  88.  */
  89. #define copy_scalar_tom(type)    *to = Exn(__load_,type,(*from));
  90. #define copy_scalar_tof(type)    Exn(__store_,type,(*to, *from));
  91.  
  92. /*
  93.  * structure member copying
  94.  */
  95. #define copy_tom(mb,type)    to->mb = Exn(__load_,type,(from->mb));
  96. #define copy_tof(mb,type)    Exn(__store_,type,(to->mb, from->mb));
  97.  
  98. /*
  99.  * structure member copying (direction independent)
  100.  */
  101. #define copy_byte(e,io,mb)    to->mb = from->mb;
  102. #define copy_addr(e,io,mb)    Ex2(copy_,io,mb,u32,e)
  103. #define copy_half(e,io,mb)    Ex2(copy_,io,mb,u16,e)
  104. #define copy_off(e,io,mb)    Ex2(copy_,io,mb,u32,e)
  105. #define copy_sword(e,io,mb)    Ex2(copy_,io,mb,i32,e)
  106. #define copy_word(e,io,mb)    Ex2(copy_,io,mb,u32,e)
  107. #define copy_arr(e,io,mb)    \
  108.     array_copy(to->mb, sizeof(to->mb), from->mb, sizeof(from->mb));
  109.  
  110. /*
  111.  * scalar copying (direction independent)
  112.  * these macros are used as `copy' arguments to copy_type()
  113.  */
  114. #define copy_addr_11(e,io,seq)    Ex1(copy_scalar_,io,u32,e)
  115. #define copy_half_11(e,io,seq)    Ex1(copy_scalar_,io,u16,e)
  116. #define copy_off_11(e,io,seq)    Ex1(copy_scalar_,io,u32,e)
  117. #define copy_sword_11(e,io,seq)    Ex1(copy_scalar_,io,i32,e)
  118. #define copy_word_11(e,io,seq)    Ex1(copy_scalar_,io,u32,e)
  119.  
  120. /*
  121.  * structure copying (direction independent)
  122.  * these macros are used as `copy' arguments to copy_type()
  123.  */
  124. #define copy_dyn_11(e,io,seq)        \
  125.     seq(copy_sword(e,io,d_tag),        \
  126.     seq(copy_addr(e,io,d_un.d_ptr),    \
  127.     /**/))
  128. #define copy_ehdr_11(e,io,seq)        \
  129.     seq(copy_arr(e,io,e_ident),        \
  130.     seq(copy_half(e,io,e_type),        \
  131.     seq(copy_half(e,io,e_machine),    \
  132.     seq(copy_word(e,io,e_version),    \
  133.     seq(copy_addr(e,io,e_entry),    \
  134.     seq(copy_off(e,io,e_phoff),        \
  135.     seq(copy_off(e,io,e_shoff),        \
  136.     seq(copy_word(e,io,e_flags),    \
  137.     seq(copy_half(e,io,e_ehsize),    \
  138.     seq(copy_half(e,io,e_phentsize),    \
  139.     seq(copy_half(e,io,e_phnum),    \
  140.     seq(copy_half(e,io,e_shentsize),    \
  141.     seq(copy_half(e,io,e_shnum),    \
  142.     seq(copy_half(e,io,e_shstrndx),    \
  143.     /**/))))))))))))))
  144. #define copy_phdr_11(e,io,seq)        \
  145.     seq(copy_word(e,io,p_type),        \
  146.     seq(copy_off(e,io,p_offset),    \
  147.     seq(copy_addr(e,io,p_vaddr),    \
  148.     seq(copy_addr(e,io,p_paddr),    \
  149.     seq(copy_word(e,io,p_filesz),    \
  150.     seq(copy_word(e,io,p_memsz),    \
  151.     seq(copy_word(e,io,p_flags),    \
  152.     seq(copy_word(e,io,p_align),    \
  153.     /**/))))))))
  154. #define copy_rela_11(e,io,seq)        \
  155.     seq(copy_addr(e,io,r_offset),    \
  156.     seq(copy_word(e,io,r_info),        \
  157.     seq(copy_sword(e,io,r_addend),    \
  158.     /**/)))
  159. #define copy_rel_11(e,io,seq)        \
  160.     seq(copy_addr(e,io,r_offset),    \
  161.     seq(copy_word(e,io,r_info),        \
  162.     /**/))
  163. #define copy_shdr_11(e,io,seq)        \
  164.     seq(copy_word(e,io,sh_name),    \
  165.     seq(copy_word(e,io,sh_type),    \
  166.     seq(copy_word(e,io,sh_flags),    \
  167.     seq(copy_addr(e,io,sh_addr),    \
  168.     seq(copy_off(e,io,sh_offset),    \
  169.     seq(copy_word(e,io,sh_size),    \
  170.     seq(copy_word(e,io,sh_link),    \
  171.     seq(copy_word(e,io,sh_info),    \
  172.     seq(copy_word(e,io,sh_addralign),    \
  173.     seq(copy_word(e,io,sh_entsize),    \
  174.     /**/))))))))))
  175. #define copy_sym_11(e,io,seq)        \
  176.     seq(copy_word(e,io,st_name),    \
  177.     seq(copy_addr(e,io,st_value),    \
  178.     seq(copy_word(e,io,st_size),    \
  179.     seq(copy_byte(e,io,st_info),    \
  180.     seq(copy_byte(e,io,st_other),    \
  181.     seq(copy_half(e,io,st_shndx),    \
  182.     /**/))))))
  183.  
  184. static void
  185. byte_copy(char *dst, const char *src, size_t n) {
  186.     if (dst == src || !n) {
  187.     return;
  188.     }
  189. #if HAVE_BROKEN_MEMMOVE
  190.     while (dst > src && dst < &src[n]) {
  191.     if (n <= 16) {
  192.         /* copy `manually' */
  193.         while (n--) {
  194.         dst[n] = src[n];
  195.         }
  196.         return;
  197.     }
  198.     /* copy upper half */
  199.     byte_copy(&dst[n / 2], &src[n / 2], n - n / 2);
  200.     /* continue with lower half */
  201.     n /= 2;
  202.     }
  203. #endif
  204.     memmove(dst, src, n);
  205. }
  206.  
  207. static void
  208. array_copy(char *dst, size_t dlen, const char *src, size_t slen) {
  209.     byte_copy(dst, src, dlen < slen ? dlen : slen);
  210.     if (dlen > slen) {
  211.     memset(dst + slen, 0, dlen - slen);
  212.     }
  213. }
  214.  
  215. /*
  216.  * instantiate copy functions
  217.  */
  218. copy_type(addr_32,,Elf32_Addr,copy_addr_11)
  219. copy_type(half_32,,Elf32_Half,copy_half_11)
  220. copy_type(off_32,,Elf32_Off,copy_off_11)
  221. copy_type(sword_32,,Elf32_Sword,copy_sword_11)
  222. copy_type(word_32,,Elf32_Word,copy_word_11)
  223. copy_type(dyn_32,11,Elf32_Dyn,copy_dyn_11)
  224. copy_type(ehdr_32,11,Elf32_Ehdr,copy_ehdr_11)
  225. copy_type(phdr_32,11,Elf32_Phdr,copy_phdr_11)
  226. copy_type(rela_32,11,Elf32_Rela,copy_rela_11)
  227. copy_type(rel_32,11,Elf32_Rel,copy_rel_11)
  228. copy_type(shdr_32,11,Elf32_Shdr,copy_shdr_11)
  229. copy_type(sym_32,11,Elf32_Sym,copy_sym_11)
  230.  
  231. typedef void (*xlator)(char*, const char*, size_t);
  232. typedef xlator xltab[ELF_T_NUM][2];
  233.  
  234. /*
  235.  * translation table (32-bit, version 1 -> version 1)
  236.  */
  237. static const xltab
  238. xlate32_11[/*encoding*/] = {
  239.     {
  240.     { byte_copy,        byte_copy        },
  241.     { addr_32L_tom,        addr_32L_tof    },
  242.     { dyn_32L11_tom,    dyn_32L11_tof   },
  243.     { ehdr_32L11_tom,   ehdr_32L11_tof  },
  244.     { half_32L_tom,        half_32L_tof    },
  245.     { off_32L_tom,        off_32L_tof        },
  246.     { phdr_32L11_tom,   phdr_32L11_tof  },
  247.     { rela_32L11_tom,   rela_32L11_tof  },
  248.     { rel_32L11_tom,    rel_32L11_tof   },
  249.     { shdr_32L11_tom,   shdr_32L11_tof  },
  250.     { sword_32L_tom,    sword_32L_tof   },
  251.     { sym_32L11_tom,    sym_32L11_tof   },
  252.     { word_32L_tom,        word_32L_tof    },
  253.     },
  254.     {
  255.     { byte_copy,        byte_copy        },
  256.     { addr_32M_tom,        addr_32M_tof    },
  257.     { dyn_32M11_tom,    dyn_32M11_tof   },
  258.     { ehdr_32M11_tom,   ehdr_32M11_tof  },
  259.     { half_32M_tom,        half_32M_tof    },
  260.     { off_32M_tom,        off_32M_tof        },
  261.     { phdr_32M11_tom,   phdr_32M11_tof  },
  262.     { rela_32M11_tom,   rela_32M11_tof  },
  263.     { rel_32M11_tom,    rel_32M11_tof   },
  264.     { shdr_32M11_tom,   shdr_32M11_tof  },
  265.     { sword_32M_tom,    sword_32M_tof   },
  266.     { sym_32M11_tom,    sym_32M11_tof   },
  267.     { word_32M_tom,        word_32M_tof    },
  268.     },
  269. };
  270.  
  271. /*
  272.  * main translation table (32-bit)
  273.  */
  274. static const xltab *const
  275. xlate32[EV_CURRENT - EV_NONE][EV_CURRENT - EV_NONE] = {
  276.     { xlate32_11, },
  277. };
  278.  
  279. #define translator(sv,dv,enc,type,d)    \
  280.     (xlate32[(sv) - EV_NONE - 1]    \
  281.         [(dv) - EV_NONE - 1]    \
  282.         [(enc) - ELFDATA2LSB]    \
  283.         [(type) - ELF_T_BYTE]    \
  284.         [d])
  285.  
  286. /*
  287.  * direction-independent translation
  288.  */
  289. static Elf_Data*
  290. elf32_xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof) {
  291.     size_t ssize, dsize, count;
  292.     Elf_Type type;
  293.     int sv, dv;
  294.     xlator op;
  295.  
  296.     if (!src || !dst) {
  297.     return NULL;
  298.     }
  299.     if (!src->d_buf || !dst->d_buf) {
  300.     seterr(ERROR_NULLBUF);
  301.     return NULL;
  302.     }
  303.     if (!valid_encoding(encode)) {
  304.     seterr(ERROR_UNKNOWN_ENCODING);
  305.     return NULL;
  306.     }
  307.     sv = src->d_version;
  308.     dv = dst->d_version;
  309.     if (!valid_version(sv) || !valid_version(dv)) {
  310.     seterr(ERROR_UNKNOWN_VERSION);
  311.     return NULL;
  312.     }
  313.     type = src->d_type;
  314.     if (!valid_type(type)) {
  315.     seterr(ERROR_UNKNOWN_TYPE);
  316.     return NULL;
  317.     }
  318.     ssize = _fmsize32(sv, type, 1 - tof);
  319.     dsize = _fmsize32(dv, type, tof);
  320.     op = translator(sv, dv, encode, type, tof);
  321.     if (!ssize || !dsize || !op) {
  322.     seterr(ERROR_UNKNOWN_TYPE);
  323.     return NULL;
  324.     }
  325.     count = src->d_size / ssize;
  326.     if (dst->d_size < count * dsize) {
  327.     seterr(ERROR_DST2SMALL);
  328.     return NULL;
  329.     }
  330.     if (count) {
  331.     (*op)(dst->d_buf, src->d_buf, count);
  332.     }
  333.     dst->d_size = count * dsize;
  334.     dst->d_type = type;
  335.     return dst;
  336. }
  337.  
  338. /*
  339.  * finally, the "official" translation functions
  340.  */
  341. Elf_Data*
  342. elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode) {
  343.     return elf32_xlate(dst, src, encode, 0);
  344. }
  345.  
  346. Elf_Data*
  347. elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode) {
  348.     return elf32_xlate(dst, src, encode, 1);
  349. }
  350.